<%@ include file="/html/init.jsp"%>

<%
	String diaChiThuongTru = ParamUtil.getString(request, "diaChiThuongTru", StringPool.BLANK);
	String soCMND = ParamUtil.getString(request, "soCMND", StringPool.BLANK);
	String soDinhDanh = ParamUtil.getString(request, "soDinhDanh", StringPool.BLANK);
	
	String tuNgay = ParamUtil.getString(request, "tuNgay", StringPool.BLANK);
	String denNgay = ParamUtil.getString(request, "denNgay", StringPool.BLANK);
	
	boolean isAbleToSearch = ParamUtil.getBoolean(request, "isAbleToSearch", true);
	
	long selTinhId = ParamUtil.getLong(request, "tinhId", 0L);
	long selQuanHuyenId = ParamUtil.getLong(request, "quanHuyenId", 0L);
	long selPhuongXaId = ParamUtil.getLong(request, "phuongXaId", 0L);
	
	// get all Tinh
	List<Tinh> tinhList = TinhLocalServiceUtil.findAll();
	
	List<CongDanJAXB> congDanList = (List<CongDanJAXB>)portletSession.getAttribute(KSDLConstants.CONG_DAN_LIST);
	
	int rowCount = 0;
	int soDanCu = congDanList.size();
	
	PortletURL kiemTraThongTinThayDoiURL = renderResponse.createRenderURL();
	kiemTraThongTinThayDoiURL.setParameter("mainTabs", KSDLConstants.IMPORT_DAN_CU_90);
	kiemTraThongTinThayDoiURL.setParameter("idcTabs", KSDLConstants.KIEM_TRA_TRUONG_THONG_TIN_BI_THAY_DOI);
%>

<portlet:actionURL name="submitContentForm" var="submitContentFormURL">
	<portlet:param name="mvcPath" value="<%=KSDLConstants.IMPORT_DAN_CU_90 %>"/>
</portlet:actionURL>

<aui:form action="<%=submitContentFormURL.toString() %>" name="contentForm" method="POST">
	<aui:input name="<%= Constants.CMD %>" type="hidden" />
	
	<aui:layout>
		<aui:column columnWidth="15">
			<label for="diaChiThuongTru"><liferay-ui:message key="dia-chi-thuong-tru"/></label>
		</aui:column>
		<aui:column columnWidth="20">
			<aui:select inlineField="<%= true %>" label="" name="tinhId" onChange="<%=portletNamespace + \"loadQuanHuyen(\'tinhId\',\'quanHuyenId\',\'phuongXaId\')\"%>">
					<aui:option label="tinh" selected="<%= true %>" value="0" />
				<%
					for (Tinh tinh: tinhList){
						long tinhId = tinh.getTinhId();
						if(tinhId == selTinhId){
				%>
					<aui:option label="<%=tinh.getTenTinh() %>" selected="<%=true %>" value="<%=tinhId %>" />
				<%
						} else {
				%>
					<aui:option label="<%=tinh.getTenTinh() %>" value="<%=tinhId %>" />
				<% 
						}
					}						
				%>
			</aui:select>
		</aui:column>
		<c:choose>
			<c:when test="<%=selTinhId > 0 %>">
				<%
					List<QuanHuyen> quanHuyenList = QuanHuyenLocalServiceUtil.findByTinhId(selTinhId);
				%>
			
				<aui:column columnWidth="20">
					<aui:select inlineField="<%= true %>" label="" name="quanHuyenId" onChange="<%=portletNamespace + \"loadPhuongXa(\'quanHuyenId\',\'phuongXaId\')\"%>">
						<aui:option label="quan-huyen" value="0" />
						
						<%
							for (QuanHuyen quanHuyen: quanHuyenList){
								long currentQuanHuyenId = quanHuyen.getQuanHuyenId();
								if(currentQuanHuyenId == selQuanHuyenId){
									
						%>
								<aui:option label="<%=quanHuyen.getTenQuanHuyen() %>" value="<%=currentQuanHuyenId %>" selected="<%= true %>"/>
						<%
								} else {
						%>
								<aui:option label="<%=quanHuyen.getTenQuanHuyen() %>" value="<%=currentQuanHuyenId %>"/>
						<% 
								}
							}						
						%>
					</aui:select>
				</aui:column>
			</c:when>
			<c:otherwise>
				<aui:column columnWidth="20">
					<aui:select inlineField="<%= true %>" label="" name="quanHuyenId" onChange="<%=portletNamespace + \"loadPhuongXa(\'quanHuyenId\',\'phuongXaId\')\"%>">
						<aui:option label="quan-huyen" selected="<%= true %>" value="0" />
					</aui:select>
				</aui:column>
			</c:otherwise>
		</c:choose>
		
		<c:choose>
			<c:when test="<%=selQuanHuyenId > 0 %>">
				<%
					List<PhuongXa> phuongXaList = PhuongXaLocalServiceUtil.findByQuanHuyenId(selQuanHuyenId);
				%>
			
				<aui:column columnWidth="20">
					<aui:select inlineField="<%= true %>" label="" name="phuongXaId" >
						<aui:option label="phuong-xa" value="0" />
						
						<%
							for (PhuongXa phuongXa: phuongXaList){
								long currentPhuongXaId = phuongXa.getPhuongXaId();
								if(currentPhuongXaId == selPhuongXaId){
									
						%>
								<aui:option label="<%=phuongXa.getTenPhuongXa() %>" value="<%=currentPhuongXaId %>" selected="<%= true %>"/>
						<%
								} else {
						%>
								<aui:option label="<%=phuongXa.getTenPhuongXa() %>" value="<%=currentPhuongXaId %>"/>
						<% 
								}
							}						
						%>
					</aui:select>
				</aui:column>
			</c:when>
			<c:otherwise>
				<aui:column columnWidth="20">
					<aui:select inlineField="<%= true %>" label="" name="phuongXaId" >
						<aui:option label="phuong-xa" value="0" />
					</aui:select>
				</aui:column>
			</c:otherwise>
		</c:choose>
		<aui:column columnWidth="20">
			<aui:input name="diaChiThuongTru" label="" inlineField="<%=true%>" type="text" placeholder="dia-chi" />
		</aui:column>
	</aui:layout>
	
	<!-- CMND + So Dinh Danh -->
	<aui:layout>
		<aui:column columnWidth="15">
			<label for="soCMND"><liferay-ui:message key="so-cmnd"/></label>
		</aui:column>
		<aui:column columnWidth="35">
			<aui:input name="soCMND" type="text" label="" id="soCMND" value="<%=soCMND %>"/>
		</aui:column>
		
		<aui:column columnWidth="15">
			<label for="soDinhDanh"><liferay-ui:message key="so-dinh-danh-ca-nhan"/></label>
		</aui:column>
		<aui:column columnWidth="35">
			<aui:input name="soDinhDanh" type="text" label="" id="soDinhDanh" value="<%=soDinhDanh %>"/>
		</aui:column>
	</aui:layout>
	
	<!-- option load du lieu: co hay ko thay doi -->
	<aui:layout>
		<aui:column columnWidth="15">
			<label for="loadDuLieu"><liferay-ui:message key="load-du-lieu"/></label>
		</aui:column>
		<aui:column columnWidth="35">
			<aui:input name="soCMND" type="text" label="" id="soCMND" value="<%=soCMND %>"/>
		</aui:column>
	</aui:layout>
	
	<!-- button block -->
	<aui:layout>
		<aui:column columnWidth="15">&nbsp;</aui:column>
		<aui:column columnWidth="85">
			<aui:button value="tim-kiem" onClick="<%= portletNamespace + \"submitContentForm(\'\"+ KSDLConstants.TIM_KIEM + \"\')\" %>" cssClass="btn-primary"/>
			<aui:button value="import-tat-ca-vao-db" onClick="<%= portletNamespace + \"submitContentForm(\'\"+ KSDLConstants.IMPORT_TAT_CA_VAO_DB + \"\')\" %>" cssClass="btn-primary"/>
			<aui:button value="in-tat-ca-thong-tin-thay-doi" onClick="<%= portletNamespace + \"submitContentForm(\'\"+ KSDLConstants.IN_TAT_CA_THONG_TIN_THAY_DOI + \"\')\" %>" cssClass="btn-primary"/>
			<aui:button value="in-tat-ca-cong-dan" onClick="<%= portletNamespace + \"submitContentForm(\'\"+ KSDLConstants.IN_TAT_CA_CONG_DAN + \"\')\" %>" cssClass="btn-primary"/>
		</aui:column>
	</aui:layout>
	
	<liferay-ui:search-container
		id="danhSachDanCuSearchContainer"
		iteratorURL="<%=PortletURLUtil.getCurrent(renderRequest, renderResponse)%>"
		curParam="hoSosParam"
		emptyResultsMessage="ko-co-cong-dan" delta="10">
		
		<liferay-ui:search-container-results
			results="<%=ListUtil.subList(congDanList, searchContainer.getStart(), searchContainer.getEnd()) %>"
			total="<%=  soDanCu%>"/> 
		
		<liferay-ui:search-container-row keyProperty="congDanId"
			className="qldc.pm.csdl.cd90.model.jaxb.CongDanJAXB" modelVar="congDan_">
			
			<%
				String soDinhDanhCN = congDan_.getSoDinhDanhCaNhan();
				String tenCongDan = StringPool.BLANK;
				String ngaySinhString = StringPool.BLANK;
				String congDanCMND = StringPool.BLANK;
				String ngayCapCMND = StringPool.BLANK;
				String gioiTinhString = StringPool.BLANK;
				String soSoHoKhau = StringPool.BLANK;
				String diaChiTT = StringPool.BLANK;
				String trangThai = LanguageUtil.get(portletConfig, locale, "ko-thay-doi");
				
				HoVaTenJAXB hoVaTen = congDan_.getHoVaTen();
				
				if(null != hoVaTen){
					tenCongDan = hoVaTen.getHoVaTen();
				}
				
				NgaySinhJAXB ngaySinh = congDan_.getNgaySinh();
				
				if(null != ngaySinh){
					ngaySinhString = KSDLUtil.parseDateToString(ngaySinh.getNgaySinh(), KSDLConstants.dd_MM_yyyy, locale);
				}
				
				CMNDJAXB cmnd = congDan_.getCmnd();
				
				if(null != cmnd){
					congDanCMND = cmnd.getSoCMND();
					ngayCapCMND = KSDLUtil.parseDateToString(cmnd.getNgayCapCMND(), KSDLConstants.dd_MM_yyyy, locale);
				}
				
				GioiTinhJAXB gioiTinh = congDan_.getGioiTinh();
				
				if(null != gioiTinh){
					int gioiTinhInt = gioiTinh.getGioiTinh();
					gioiTinhString = (gioiTinhInt == EnumGioiTinh.NAM.getValue()) ? EnumGioiTinh.NAM.getKey() : EnumGioiTinh.NU.getKey();
				
					gioiTinhString = LanguageUtil.get(portletConfig, locale, gioiTinhString);
				}
				
				QuanHeVoiChuHoJAXB quanHe = congDan_.getQuanHeVoiChuHo();
				
				if(null != quanHe){
					soSoHoKhau = quanHe.getSoSoHoKhau();
				}
				
				NoiThuongTruJAXB noiThuongTru = congDan_.getNoiThuongTru();
				
				if(null != noiThuongTru){
					diaChiTT = DiaChiUtils.createBlockDiaChi(noiThuongTru.getTinhId(), noiThuongTru.getQuanHuyenId(), noiThuongTru.getPhuongXaId(), noiThuongTru.getDiaChi()); 
				}
				
				CongDanJAXB congDanTrongDB = CongDanJAXBUtil.convertDBObjectArrayToCongDanJAXB(CongDanLocalServiceUtil.findCongDanAndItsAttributesBySDDCN(soDinhDanhCN));
				boolean thayDoi = CongDanJAXBUtil.checkThayDoi(congDan_, congDanTrongDB);
				
				if(thayDoi){
					trangThai = LanguageUtil.get(portletConfig, locale, "co-thay-doi");
				}
				
				kiemTraThongTinThayDoiURL.setParameter("soDinhDanh", soDinhDanhCN);
			%>
			
			<liferay-ui:search-container-column-text cssClass="aui-w3" name="stt" value="<%= Integer.toString(searchContainer.getStart()+(++rowCount)) %>" />
			<liferay-ui:search-container-column-text name="so-ddcn" value="<%=soDinhDanhCN %>"/>
			<liferay-ui:search-container-column-text name="ho-va-ten" value="<%=tenCongDan %>" />
 			<liferay-ui:search-container-column-text name="ngay-sinh" value="<%=ngaySinhString %>" />
			<liferay-ui:search-container-column-text name="so-cmnd" value="<%=soCMND %>"/>
			<liferay-ui:search-container-column-text name="ngay-cap-cmnd" value="<%=ngayCapCMND %>"/>
			<liferay-ui:search-container-column-text name="gioi-tinh" value="<%=gioiTinhString %>"/>
			<liferay-ui:search-container-column-text name="so-ho-khau" value="<%=soSoHoKhau %>"/>
			<liferay-ui:search-container-column-text name="dia-chi-thuong-tru" value="<%=diaChiTT %>"/>
			<liferay-ui:search-container-column-text name="trang-thai" value="<%=trangThai %>"/>
			
			<liferay-ui:search-container-column-text name="thao-tac" cssClass="aui-w8 aui-Action">
				<liferay-ui:icon-menu message="thao-tac">
					<liferay-ui:icon image="view" message="kiem-tra-truong-thong-tin-bi-thay-doi" url="<%=kiemTraThongTinThayDoiURL.toString()%>"/>
				  	<liferay-ui:icon image="permissions" message="xem-lich-su-thay-doi" url="<%=PortalUtil.getCurrentURL(request)%>" />
				  	<liferay-ui:icon image="permissions" message="in-bao-cao-thay-doi" url="<%=PortalUtil.getCurrentURL(request)%>" />
				  	<liferay-ui:icon image="permissions" message="in-chi-tiet-cong-dan-90" url="<%=PortalUtil.getCurrentURL(request)%>" />
				  	<liferay-ui:icon image="permissions" message="import-vao-co-so-du-lieu" url="<%=PortalUtil.getCurrentURL(request)%>" />
				  	<liferay-ui:icon image="permissions" message="xuat-bang-so-sanh-su-thay-doi-ra-file" url="<%=PortalUtil.getCurrentURL(request)%>" />
				</liferay-ui:icon-menu>
			</liferay-ui:search-container-column-text>
		</liferay-ui:search-container-row>
		<liferay-ui:search-iterator paginate="true"/>
	</liferay-ui:search-container>
</aui:form>

<script type="text/javascript">
window[ksdl_namespace + 'submitContentForm'] = function(cmd) {
	$('#' + ksdl_namespace + 'cmd').val(cmd);

	if(cmd.startsWith('<%=KSDLConstants.IN%>')){
		$('#' + ksdl_namespace + 'submitContentForm').attr("target", "_blank");
	} else {
		$('#' + ksdl_namespace + 'submitContentForm').removeAttr("target");
	}
	
	$('#' + ksdl_namespace + 'submitContentForm').submit();
};
</script>
